package com.xjrsoft.module.app.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.common.allenum.EnabledMarkEnum;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.app.entity.XjrAppVersion;
import com.xjrsoft.module.app.mapper.XjrAppVersionMapper;
import com.xjrsoft.module.app.service.IXjrAppVersionService;
import com.xjrsoft.module.app.vo.AppVersionVo;
import com.xjrsoft.module.base.entity.XjrBaseAnnexesFile;
import com.xjrsoft.module.base.service.IXjrBaseAnnexesFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zjq
 * @since 2022-08-29
 */
@Service
@AllArgsConstructor
public class XjrAppVersionServiceImpl extends ServiceImpl<XjrAppVersionMapper, XjrAppVersion> implements IXjrAppVersionService {

    XjrAppVersionMapper xjrAppVersionMapper;

    IXjrBaseAnnexesFileService iXjrBaseAnnexesFileService;


    @Override
    public AppVersionVo getAppVersionById(String id) {
        XjrAppVersion xjrAppVersion = xjrAppVersionMapper.selectById(id);
        if (xjrAppVersion != null) {
            AppVersionVo appVersionVo = BeanUtil.copy(xjrAppVersion, AppVersionVo.class);
            String fileId = xjrAppVersion.getFileId();
            XjrBaseAnnexesFile xjrBaseAnnexesFile = iXjrBaseAnnexesFileService.getById(fileId);
            if (xjrBaseAnnexesFile != null) {
                String fileName = xjrBaseAnnexesFile.getFileName();
                appVersionVo.setFileName(fileName);
            }
            return appVersionVo;
        }
        return null;
    }

    @Override
    public AppVersionVo getNew() {
        List<XjrAppVersion> list = this.list(Wrappers.<XjrAppVersion>query().lambda()
                        .eq(XjrAppVersion::getEnabledMark, EnabledMarkEnum.ENABLED.getCode())
                        .orderByDesc(XjrAppVersion::getCreateDate));
        XjrAppVersion xjrAppVersion = list.get(0);
        if(xjrAppVersion.getUrl()==null){
            AppVersionVo appVersionVo = BeanUtil.copy(xjrAppVersion, AppVersionVo.class);
            String fileId = xjrAppVersion.getFileId();
            XjrBaseAnnexesFile xjrBaseAnnexesFile = iXjrBaseAnnexesFileService.getById(fileId);
            if (xjrBaseAnnexesFile != null) {
                String fileName = xjrBaseAnnexesFile.getFileName();
                appVersionVo.setFileName(fileName);
            }
            return appVersionVo;
        }
           return BeanUtil.copy(xjrAppVersion, AppVersionVo.class);

    }
}
